package org.afun.csms_android.adapter;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Parcelable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.RecyclerView;

import org.afun.csms_android.R;
import org.afun.csms_android.UserAlterActivity;
import org.afun.csms_android.domain.User;
import org.afun.csms_android.utils.MyTableOperator;
import org.afun.csms_android.utils.TableOperator;

import java.util.List;

/**
 * 用户类适配器，将用户信息展示在ManagerHome上
 * 管理员查看所有用户信息
 */

public class UserAdapter extends RecyclerView.Adapter<UserAdapter.ViewHolder> {

    private List<User> mUserList;
    private Context context;

    static class ViewHolder extends RecyclerView.ViewHolder {
        View UserView;
        ImageView UserImage;
        TextView UserMsg;
        Button alterBtn;
        Button deleteBtn;

        public ViewHolder(View view) {
            super(view);
            UserView = view;
            UserImage = (ImageView) view.findViewById(R.id.User_image);
            UserMsg = view.findViewById(R.id.User_msg);
            alterBtn = view.findViewById(R.id.userAlterBtn);
            deleteBtn = view.findViewById(R.id.userDeleteBtn);
        }

    }

    public UserAdapter(List<User> userList, Context context) {
        mUserList = userList;
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.user_item, parent, false);
        final ViewHolder holder = new ViewHolder(view);
        holder.UserView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                User user = mUserList.get(position);
                Toast.makeText(v.getContext(), "you clicked view " + user.getId(), Toast.LENGTH_SHORT).show();
            }
        });
        holder.UserImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                User user = mUserList.get(position);
                Toast.makeText(v.getContext(), "you clicked image " + user.getId(), Toast.LENGTH_SHORT).show();
            }
        });

        holder.alterBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int postion = holder.getAdapterPosition();
                User user = mUserList.get(postion);
                Intent intent = new Intent(context, UserAlterActivity.class);
                intent.putExtra("userId", user.getId());
                intent.putExtra("userAccount", user.getAccount());
                intent.putExtra("userName", user.getName());
                intent.putExtra("userPwd", user.getPassword());
                String roleName = "";
                switch (user.getRoleId()) {
                    case 1:
                        roleName = "管理员";
                        break;
                    case 2:
                        roleName = "教师";
                        break;
                    case 3:
                        roleName = "学生";
                        break;
                    default:
                        break;
                }
                intent.putExtra("userRole", roleName);
                intent.putExtra("userGender", user.getGender());
                context.startActivity(intent);
//                Toast.makeText(v.getContext(), "you clicked alterBtn " + user.getId(), Toast.LENGTH_SHORT).show();
            }
        });

        holder.deleteBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int postion = holder.getAdapterPosition();
                User user = mUserList.get(postion);
                new AlertDialog.Builder(context).setTitle("提示")
                        .setMessage("你是否确定删除id为" + user.getId() + "的用户？")
                        .setPositiveButton("是", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                MyTableOperator myTableOperator = new MyTableOperator(context);
                                TableOperator tableOperator = myTableOperator.getTableOperator();
                                boolean b = tableOperator.deleteUserById(user.getId());
                                if (b) {
                                    new androidx.appcompat.app.AlertDialog.Builder(context)
                                            .setTitle("提示信息")
                                            .setMessage("删除成功！")
                                            .setPositiveButton("确定", null)
                                            .create()
                                            .show();
                                    mUserList.remove(postion);
                                    notifyItemRemoved(postion);
                                    notifyItemRangeChanged(postion, mUserList.size());
                                } else {
                                    new androidx.appcompat.app.AlertDialog.Builder(context)
                                            .setTitle("提示信息")
                                            .setMessage("删除失败...")
                                            .setPositiveButton("确定", null)
                                            .create()
                                            .show();
                                }

                            }
                        })
                        .setNegativeButton("否", null)
                        .show();

//                Toast.makeText(v.getContext(), "you clicked deleteBtn " + user.getId(), Toast.LENGTH_SHORT).show();
            }
        });

        return holder;
    }

    public void onBindViewHolder(ViewHolder holder, int position) {
        User user = mUserList.get(position);
        holder.UserImage.setImageResource(R.drawable.name);
        int roleId = user.getRoleId();
        String roleName = null;
        switch (roleId) {
            case 1:
                roleName = "管理员";
                break;
            case 2:
                roleName = "教师";
                break;
            case 3:
                roleName = "学生";
                break;
            default:
                roleName = "";
                break;
        }

        holder.UserMsg.setText(
                "用户号：" + user.getId()
                        + "\n账号：" + user.getAccount()
                        + "\n姓名：" + user.getName()
                        + "\n密码：" + user.getPassword()
                        + "\n性别：" + user.getGender()
                        + "\n身份：" + roleName);

    }

    @Override
    public int getItemCount() {
        return mUserList.size();
    }

}
